package io.mft.commons.db.mapper.transaction;

import cn.hutool.db.Session;

public class MapperDbOperator {
	private MapperTransaction mapperTransaction;
	private String datasourceId;
	private Session session;
	private boolean auto;
	public MapperDbOperator(MapperTransaction mapperTransaction,Session session,String datasourceId,boolean auto) {
		this.mapperTransaction=mapperTransaction;
		this.datasourceId=datasourceId;
		this.auto=auto;
		if (auto) {
			mapperTransaction.begin();
		}
		((MapperTransactionSessionHandler)mapperTransaction).attachSession(datasourceId, session);
		this.session=session;
	}
	public boolean isAuto() {
		return auto;
	}

	public Session getSession() {
		return session;
	}
	public void beginTrans() {
		((MapperTransactionSessionHandler)mapperTransaction).begin(datasourceId);
	}
	
	public void commitTrans() {
		mapperTransaction.commit();
	}
	
	public void rollbackTrans() {
		mapperTransaction.rollback();
	}
	public void closeTrans() {
		mapperTransaction.close();
	}
}
